package com.hrbxlh.action;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.CoursePowerActionImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/4.
 */
@Controller
public class CoursePowerAction {

    @Autowired
    @Qualifier("coursePowerActionImpl")
    private CoursePowerActionImpl coursePowerActionImpl;

    @Autowired
    @Qualifier("courseActionImpl")
    private CourseActionImpl courseActionImpl;

    /**
     * @description 能力管理列表
     * @param request
     * @param response
     * @return  跳转到能力管理列表页面
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/skipCoursePowerMana", method = RequestMethod.POST)
    public void skipCoursePowerMana(String jsonStr, HttpServletResponse response)   {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(SiteAction.class.getName());
        try {
            Map<String,Object> preParamMap = new HashMap<String, Object>();
            //获取参数值
            if(jsonStr != null){
                preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
            }
            if(courseActionImpl.getMapSpecialMark(response, mapper, resultMap, preParamMap)) return;
            //获取能力列表
            Map<String, Object> coursePowerMap = coursePowerActionImpl.getCoursePowerList(preParamMap);
            if(coursePowerMap != null){
                resultMap.put("coursePowerList", coursePowerMap.get("coursePowerList"));
                resultMap.put("totalPage", coursePowerMap.get("totalPage"));
                resultMap.put("pageNow", coursePowerMap.get("pageNow"));
            }
            //返回条件
            resultMap.put("pageSize", preParamMap.get("pageSize"));
            resultMap.put("powerName", preParamMap.get("powerName"));
            resultMap.put("result", "success");
            resultMap.put("failMsg","");
            courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap) );
        } catch (Exception e) {
            resultMap.put("result", "fail");
            resultMap.put("failMsg","跳转到能力管理列表页面出现异常");
            e.printStackTrace();
            logger.error("跳转到能力管理列表页面出现异常skipCoursePowerMana", e);
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap) );
            } catch (JsonProcessingException e1) {
                e1.printStackTrace();
            }
        }
    }


    /**
     * 添加能力数据
     * @param response
     * @param jsonStr {"powerList":[{"powerName":"aa","relCourseList":[{"courseId":1,"width":0.6}]}]}
     */
    @RequestMapping(value = "/addCousrPower", method = RequestMethod.POST)
    public void  addCousrPower (HttpServletResponse response, String jsonStr)   {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(SiteAction.class.getName());
        //成功失败标识
        String resultMark = "fail";
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            if(courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
            Map<String, Object> addResultMap = coursePowerActionImpl.addCousrPower(paramMap);
            Boolean flag = (Boolean)addResultMap.get("result");
            //返回参数
            resultMark = (flag) ? "success" : "fail";
            resultMap.put("result", resultMark);
            resultMap.put("failMsg", addResultMap.get("failMsg"));
            resultMap.put("powerId",addResultMap.get("powerId"));
            courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
        } catch (Exception e) {
            resultMap.put("result", "fail");
            e.printStackTrace();
            logger.error("添加能力数据出现异常addCousrPower", e);
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
            } catch (JsonProcessingException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 添加课程权重
     * @param response
     * @param jsonStr {"ourseWidthMap":{"expreWidth":0.2,"homeworkWidth":0.3,"examWidth":0.5}}
     */
    @RequestMapping(value = "/addCourseWidth", method = RequestMethod.POST)
    public void  addCourseWidth (HttpServletResponse response, String jsonStr)   {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(SiteAction.class.getName());
        //成功失败标识
        String resultMark = "fail";
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            if(courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
            Map<String, Object> addResultMap = coursePowerActionImpl.addCourseWidth(paramMap);
            Boolean flag = (Boolean)addResultMap.get("result");
            //返回参数
            resultMark = (flag) ? "success" : "fail";
            resultMap.put("result", resultMark);
            resultMap.put("failMsg", addResultMap.get("failMsg"));
            courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
        } catch (Exception e) {
            resultMap.put("result", "fail");
            e.printStackTrace();
            logger.error("添加能力数据出现异常addCousrPower", e);
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
            } catch (JsonProcessingException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 进入修改能力页面
     * @param jsonStr
     * @param response
     */
    @RequestMapping(value = "/skipSaveCoursePower", method = RequestMethod.POST)
    public void skipSaveCoursePower(String jsonStr, HttpServletResponse response)   {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(SiteAction.class.getName());
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            Map<String,Object> coursePowerDetail = coursePowerActionImpl.getCoursePowerDetail(paramMap);
            resultMap.put("coursePowerDetail", coursePowerDetail);
            resultMap.put("result", "success");
        } catch (Exception e) {
            resultMap.put("result", "fail");
            e.printStackTrace();
            logger.error("跳修改能力页面出现异常skipSaveCoursePower", e);
        } finally {
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 修改能力数据
     * @param response
     * @param jsonStr {"powerList":[{"powerId":8,"powerName":"aaa","addRelCourseList":[{"courseId":1,"width":0.6,"expreWidth":0.2,"homeworkWidth":0.3,"examWidth":0.5}],
     *                              delRelCourseList":[{"courseId":1,"width":0.6,"expreWidth":0.2,"homeworkWidth":0.3,"examWidth":0.5}]}]}
     */
    @RequestMapping(value = "/saveCoursePower", method = RequestMethod.POST)
    public void  saveCoursePower (HttpServletResponse response, String jsonStr)   {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(SiteAction.class.getName());
        //成功失败标识
        String resultMark = "fail";
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            if(courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
            //修改修改能力数据
            Map<String, Object> saveResultMap = coursePowerActionImpl.saveCoursePower(paramMap);
            Boolean flag = (Boolean)saveResultMap.get("result");
            //返回参数
            resultMark = (flag) ? "success" : "fail";
            resultMap.put("result", resultMark);
            resultMap.put("failMsg", saveResultMap.get("failMsg"));
            courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
        } catch (Exception e) {
            resultMap.put("result", "fail");
            e.printStackTrace();
            logger.error("修改能力数据出现异常saveCoursePower", e);
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
            } catch (JsonProcessingException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * @description 删除能力
     * @param response
    //jsonStr = ["1","2","3"]
     * @return  能力id
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/delCoursePowers", method = RequestMethod.POST)
    public void  delCoursePowers (HttpServletResponse response, String jsonStr)   {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(SiteAction.class.getName());
        //成功失败标识
        String resultMark = "fail";
        List<String> list = new ArrayList<String>();
        try {
            //获取参数值
            list = mapper.readValue(jsonStr, list.getClass());
            //删除能力
            Map<String, Object> result = coursePowerActionImpl.delCoursePowers(list);
            boolean resultMarkUse = (Boolean)result.get("result");
            //返回参数
            resultMark = (resultMarkUse) ? "success" : "fail";
            resultMap.put("result", resultMark);
            resultMap.put("failMsg",result.get("failMsg"));
        } catch (Exception e) {
            resultMap.put("result", "fail");
            e.printStackTrace();
            logger.error("删除能力出现异常delSites", e);
        } finally {
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @description 获取删除课程的时候是否提示有课程与能力值关联
     * @param 课程id集合
     * */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getRelCoPoListByCourseList", method = RequestMethod.POST)
    public void  getRelCoPoListByCourseList (HttpServletResponse response, String jsonStr) {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(CourseAction.class.getName());
        //用于转换参数
        List<String> list = new ArrayList<String>();
        try {
            //获取参数值
            List<String> paramList = mapper.readValue(jsonStr, list.getClass());
            //查询这些课程是否有课程与能力值关联
            resultMap = coursePowerActionImpl.getRelCoPoListByCourseList(paramList);
        } catch (Exception e) {
            resultMap.put("result", "fail");
            e.printStackTrace();
            logger.error("获取删除课程的时候是否提示有课程与能力值关联出现异常getRelCoPoListByCourseList", e);
        } finally {
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @description 获取课程中各个模块的权重
     * @param
     * */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getCourseWidth", method = RequestMethod.POST)
    public void  getCourseWidth (HttpServletResponse response) {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(CourseAction.class.getName());
        try {
            //查询这些课程是否有课程与能力值关联
            resultMap = coursePowerActionImpl.getCourseWidth();
        } catch (Exception e) {
            resultMap.put("result", "fail");
            e.printStackTrace();
            logger.error("获取课程中各个模块的权重出现异常getCourseWidth", e);
        } finally {
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @description 获取所有课程
     * @param
     * */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getAllCourseWithCoursePower", method = RequestMethod.POST)
    public void  getAllCourseWithCoursePower (HttpServletResponse response) {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //日志信息
        Logger logger = Logger.getLogger(CourseAction.class.getName());
        try {
            Map<String,Object> preParamMap = new HashMap<String,Object>();
            preParamMap.put("releaseMark",1);
            List<Map<String, Object>> courseList = courseActionImpl.getAllCourse(preParamMap);
            resultMap.put("courseList", courseList);
        } catch (Exception e) {
            resultMap.put("result", "fail");
            e.printStackTrace();
            logger.error("获取所有课程出现异常getAllCourseWithCoursePower", e);
        } finally {
            try {
                courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }
}
